package com.example.forum.common;


import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
public class AppResult<T> {
    //状态码
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private int code;
    //描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    //返回的数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;
    //总数，用于分页
    @JsonInclude(JsonInclude.Include.NON_DEFAULT)
    private int total;

    public AppResult(int code, String message) {
        this(code,message,null);
    }

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.total = 0;
    }
    //成功时调用
    public static AppResult success(){
        return new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }
    //成功时 自定义描述信息
    public static AppResult success(String message){
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }
    //成功时返回的数据
    public static <T>AppResult<T> success(T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }
    //成功时 返回数据和自定义描述信息
    public static <T>AppResult<T> success(String message,T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),message,data);
    }
    //成功时 返回数据和分页总数
    public static <T>AppResult<T> success(T data, int total){
        AppResult<T> result = new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
        result.setTotal(total);
        return result;
    }
    //失败时调用
    public static AppResult failed(){
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }
    //失败时调用 传入错误信息
    public static AppResult failed(String message){
        return new AppResult(ResultCode.FAILED.getCode(),message);
    }
    //失败时调用 传入失败对象
    public static AppResult failed(ResultCode resultCode){
       return new AppResult(resultCode.getCode(),resultCode.getMessage());
    }
    
    //失败时调用 传入失败对象和自定义错误信息
    public static AppResult failed(ResultCode resultCode, String message){
       return new AppResult(resultCode.getCode(), message);
    }

}
